SpringBoot 整合 druid Monitor进行sql监控 您所在的位置:网站首页 druid you are not permitted SpringBoot 整合 druid Monitor进行sql监控

SpringBoot 整合 druid Monitor进行sql监控

2023-09-10 06:47| 来源: 网络整理| 查看: 265

使用springboot集成druid-Monitor进行sql监控、数据源监控,sql慢查询监控。

一、前言

软件架构:

springboot框架druid地址池mybatis Druid 简介

Druid 是阿里巴巴开源的数据库连接池,提供了优秀的对数据库操作的监控功能。

Druid的好并不止体现在作为一个连接池加快数据访问性能上和连接管理上,他带有一个强大的监控工具:Druid Monitor。 不仅可以监控数据源和慢查询,还可以监控Web应用、URI监控、Session监控、Spring监控。

二、实现过程 1.引入依赖 org.springframework.boot spring-boot-starter-web com.alibaba druid 1.1.20 com.alibaba druid-spring-boot-starter 1.1.20 org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true com.baomidou mybatis-plus-boot-starter 3.4.3 mysql mysql-connector-java runtime 2.配置相关属性

配置Druid数据源(连接池): 如同以前 c3p0、dbcp 数据源可以设置数据源连接初始化大小、最大连接数、等待时间、最小连接数 等一样,Druid 数据源同理可以进行设置;

配置 Druid web 监控 filter(WebStatFilter): 这个过滤器的作用就是统计 web 应用请求中所有的数据库信息,比如 发出的 sql 语句,sql 执行的时间、请求次数、请求的 url 地址、以及seesion 监控、数据库表的访问次数 等等。

配置 Druid 后台管理 Servlet(StatViewServlet): Druid 数据源具有监控的功能,并提供了一个 web 界面方便用户查看,类似安装 路由器 时,人家也提供了一个默认的 web 页面;需要设置 Druid 的后台管理页面的属性,比如 登录账号、密码 等;

注意:

Druid Spring Boot Starter 配置属性的名称完全遵照 Druid,可以通过 Spring Boot配置文件来配置Druid数据库连接池和监控,如果没有配置则使用默认值。

application.yml配置如下:

# 数据源配置 spring: datasource: ######### JDBC 基本配置 ######### name: test url: jdbc:mysql://localhost:3306/db_user?&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSl=false username: root password: root driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 filters: stat,wall,log4j # 通过connectProperties属性来打开mergeSql功能;慢SQL记录 connectionProperties: druid.stargeSql=true;druid.stat.slowSqlMillis=5000 # 超过时间限制是否回收 removeAbandoned: true # 超时时间;单位为秒。180秒=3分钟 removeAbandonedTimeout: 180 # 关闭abanded连接时输出错误日志 logAbandoned: true ## 配置连接池信息 druid: ## 初始化大小,最小,最大 initial-size: 5 min-idle: 5 max-active: 30 ## 配置获取连接等待超时的时间 max-wait: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 time-between-eviction-runs-millis: 60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 min-evictable-idle-time-millis: 300000 max-pool-prepared-statement-per-connection-size: 20 pool-prepared-statements: true # 用来测试连接是否可用的SQL语句 validation-query: SELECT 1 FROM DUAL # 应用向连接池申请连接,并且testOnBorrow为false时,连接池将会判断连接是否处于空闲状态,如果是,则验证这条连接是否可用 test-while-idle: true # 如果为true,默认是false,应用向连接池申请连接时,连接池会判断这条连接是否是可用的 test-on-borrow: false # 如果为true(默认false),当应用使用完连接,连接池回收连接的时候会判断该连接是否还可用 test-on-return: false ######### 配置statViewFilter(配置Druid后台管理Servlet页面),用于展示Druid的统计信息 stat-view-servlet: # sql监控开关 enabled: true # 访问内置监控页面的路径,内置监控页面的首页是/druid/index.html url-pattern: /druid/* # 是否允许清空统计数据,重新计算 true:允许 false:不允许 reset-enable: true # 配置监控页面访问账号密码 login-username: admin login-password: admin # 允许访问的地址,如果allow没有配置或者为空,则允许所有访问 allow: 127.0.0.1 # 拒绝访问的地址,deny优先于allow,如果在deny列表中,就算在allow列表中,也会被拒绝 deny: ######### 配置WebStatFilter,用于采集web关联监控的数据######### web-stat-filter: # web url监控 enabled: true # session监控 session-stat-enable: true # session的最大个数,默认100 session-stat-max-count: 1000 # 过滤所有url url-pattern: /* # 排除一些不必要的url exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*" # 自己配置监控统计拦截的filter filter: # 开启DruidDataSource的状态监控 stat: enabled: true db-type: mysql # 开启慢sql监控,超过2s 就认为是慢sql,记录到日志中 log-slow-sql: true slow-sql-millis: 2000 # 日志配置(xml扩展配置) logPath: D:/logs/ logName: admin logBusinessLevel: debug logRootLevel: info #logging: # config: classpath:logback-spring.xml

上述配置文件的参数可以在 :com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties 和 org.springframework.boot.autoconfigure.jdbc.DataSourceProperties中找到;

在这里插入图片描述 2.1 如何配置 Filter

可以通过 spring.datasource.druid.filters=stat,wall,log4j …的方式来启用相应的内置Filter,不过这些Filter都是默认配置。如果默认配置不能满足需求,可以放弃这种方式,通过配置文件来配置Filter,下面是例子。

3.监控页面

(1)启动项目后,访问 ip:端口号/druid/login.html来登录页面,输入 配置文件中 设置的 账号和密码 在这里插入图片描述 (2)数据源页面 是当前DataSource配置的基本信息,上述配置的Filter可以在里面找到,如果没有配置Filter(一些信息会无法统计,例如“SQL监控”,会无法获取JDBC相关的SQL执行信息) 在这里插入图片描述 (3)SQL监控页面 SQL监控页面统计了所有SQL语句的执行情况。

没有请求前,数据源和sql监控页面都是空的。 在这里插入图片描述 我们生成一个查询请求:http://localhost:8086/user/listAll,然后刷新druid-Monitor中的数据源窗口和sql监控窗口 在这里插入图片描述

(4)URL监控页面

URL监控页面统计了所有Controller接口的访问以及执行情况 在这里插入图片描述 (5)Spring 监控页面

Spring 监控页面 利用aop 对指定 接口的执行时间,jdbc数进行记录。 在这里插入图片描述

4.去除广告图片 package com.example.druiddemo.config; import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties; import com.alibaba.druid.util.Utils; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.servlet.*; import java.io.IOException; /** * 除去页面底部的广告 * @author qzz */ @Configuration @ConditionalOnWebApplication @AutoConfigureAfter(DruidDataSourceAutoConfigure.class) @ConditionalOnProperty(name = "spring.datasource.druid.stat-view-servlet.enabled", havingValue = "true", matchIfMissing = true) public class RemoveDruidAdConfig { /** * 除去页面底部的广告 * @param properties * @return */ @Bean public FilterRegistrationBean removeDruidAdfilterRegistrationBean(DruidStatProperties properties){ //获取web监控页面的参数 DruidStatProperties.StatViewServlet config = properties.getStatViewServlet(); //提取common.js的配置路径 String pattern = config.getUrlPattern()!=null?config.getUrlPattern():"/druid/*"; String commonJsPattern = pattern.replaceAll("\\*","js/common.js"); final String filePath = "support/http/resources/js/common.js"; //创建filter进行过滤 Filter filter = new Filter() { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { chain.doFilter(request,response); //重置缓冲区,响应不会被重置 response.resetBuffer(); //获取common.js String text = Utils.readFromResource(filePath); //正则替换banner,除去底部的广告信息 text = text.replaceAll("


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有